/**
 *  Copyright (c) 1997-2013, www.tinygroup.org (luo_guo@icloud.com).
 *
 *  Licensed under the GPL, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.gnu.org/licenses/gpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.xnck.maventest.dao;

import static org.tinygroup.tinysqldsl.base.StatementSqlBuilder.and;
import static com.xnck.maventest.dao.AccountTable.*;
import static org.tinygroup.tinysqldsl.Select.*;
import static org.tinygroup.tinysqldsl.Insert.*;
import static org.tinygroup.tinysqldsl.Delete.*;
import static org.tinygroup.tinysqldsl.Update.*;

import java.util.List;

import org.springframework.stereotype.Component;
import org.tinygroup.tinysqldsl.Delete;
import org.tinygroup.tinysqldsl.Insert;
import org.tinygroup.tinysqldsl.Select;
import org.tinygroup.tinysqldsl.Update;
import org.tinygroup.tinysqldsl.Pager;
import org.tinygroup.tinysqldsl.extend.MysqlSelect;

import com.xnck.maventest.entity.Account;

@Component
public class AccountDaoImpl extends BaseTwoDao {

	public Account insertAccount(Account account) {
		Insert insert = insertInto(ACCOUNT_INFO).values(
				ACCOUNT_INFO.ID.value(account.getId()),
				ACCOUNT_INFO.ACCOUNT.value(account.getAccount()),
				ACCOUNT_INFO.USERID.value(account.getUserId()));
		dslSession.execute(insert);
		return account;
	}

	public int updateAccount(Account account) {
		Update update = update(ACCOUNT_INFO).set(
				ACCOUNT_INFO.ACCOUNT.value(account.getAccount()),
				ACCOUNT_INFO.USERID.value(account.getUserId())).where(
						ACCOUNT_INFO.ID.eq(account.getId()));
		return dslSession.execute(update);
	}

	public int deleteAccount(String pk) {
		Delete delete = delete(ACCOUNT_INFO).where(ACCOUNT_INFO.ID.eq(pk));
		return dslSession.execute(delete);
	}

	public int deleteAccounts(Object... pks) {
		Delete delete = delete(ACCOUNT_INFO).where(ACCOUNT_INFO.ID.in(pks));
		return dslSession.execute(delete);
	}

	@SuppressWarnings({"rawtypes" })
	public Account getAccountById(String pk) {
		Select select = selectFrom(ACCOUNT_INFO).where(ACCOUNT_INFO.ID.eq(pk));
		return dslSession.fetchOneResult(select, Account.class);
	}

	@SuppressWarnings({"rawtypes" })
	public List<Account> queryAccounts(Account account) {
		if(account==null){
			account=new Account();
		}
		Select select = selectFrom(ACCOUNT_INFO).where(
				and(
						ACCOUNT_INFO.ACCOUNT.eq(account.getAccount()),
						ACCOUNT_INFO.USERID.eq(account.getUserId())));
		return dslSession.fetchList(select, Account.class);
	}

	@SuppressWarnings({"rawtypes" })
	public Pager<Account> queryAccountsForPage(int start,int limit ,Account account) {
		if(account==null){
			account=new Account();
		}
		Select select = MysqlSelect.selectFrom(ACCOUNT_INFO).where(
				and(
						ACCOUNT_INFO.ACCOUNT.eq(account.getAccount()),
						ACCOUNT_INFO.USERID.eq(account.getUserId())));
		return dslSession.fetchPage(select,start, limit, false, Account.class);
	}

}
